package com.wushi.lenovo.asia5b.app_di.credit.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.TransferEmailActivityView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhangyuncai on 2019/1/16.
 */
public class TransferEmailActivityPresenter extends BasePresenter<TransferEmailActivityView> {

    ApiService apiService;

    @Inject
    public TransferEmailActivityPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void searchEmail(String email) {
        Disposable subscribe = apiService.searEmail(UserInfo.getMemberId(), email)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    getView().loading(true);
                })
                .doAfterTerminate(() -> {
                    getView().loading(false);
                })
                .subscribe(resultModel -> {
                    getView().searEmailResult(resultModel);
                    if (!resultModel.isSuccess()) {
                        ToastUtil.showToast(resultModel.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
        composite.add(subscribe);
    }

}